package com.dandelion.manager.system.service.impl;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dandelion.commons.spring.SpringContextHolder;
import com.dandelion.commons.utils.StringUtils;
import com.dandelion.commons.web.model.AjaxResponse;
import com.dandelion.manager.system.service.MonitorService;
import com.dandelion.manager.system.web.form.MoitorThreadinfo;
import com.dandelion.manager.system.web.form.MoitorThreadinfoResult;
import com.dandelion.manager.system.web.form.MonitorCacheinfo;

@Service
public class MonitorServiceImpl implements MonitorService {

	@Autowired
	private CacheManager cacheManager;

	@Override
	public List<MonitorCacheinfo> getCacheInfo() {
		List<MonitorCacheinfo> list = new ArrayList<MonitorCacheinfo>();
		String[] cacheNames = cacheManager.getCacheNames();
		int length = cacheNames.length;
		for (int i = 0; i < length; i++) {
			String cacheName = cacheNames[i];
			Cache cache = cacheManager.getCache(cacheName);
			MonitorCacheinfo moitorCacheinfo = new MonitorCacheinfo();
			moitorCacheinfo.setName(cacheName);
			long cacheHitCount = cache.getStatistics().getCacheHits(); // 命中次数
			long cacheMissCount = cache.getStatistics().getCacheMisses(); // 失效次数
			long totalCount = cacheHitCount + cacheMissCount; //
			long cacheHitPercent = 100;
			if (totalCount != 0) {
				cacheHitPercent = cacheHitCount * 100 / totalCount; // 总命中率
			}
			moitorCacheinfo.setTotalCount(totalCount);
			moitorCacheinfo.setCacheHits(cacheHitCount);
			moitorCacheinfo.setCacheMisses(cacheMissCount);
			moitorCacheinfo.setCacheHitPercent(cacheHitPercent + " %");
			list.add(moitorCacheinfo);
		}
		return list;
	}
	
	@Override
	public AjaxResponse freshenCache(String cacheNames) {
		AjaxResponse ajaxResponse = SpringContextHolder.getBean(AjaxResponse.class);
		if (StringUtils.isNotBlank(cacheNames)) {
			String[] names = cacheNames.split(",");
			for (String cacheName : names) {
				Cache cache = cacheManager.getCache(cacheName) ;
				cache.removeAll();
				cache.clearStatistics();
			}
		}
		return ajaxResponse;
	}

	@Override
	public MoitorThreadinfoResult getThreadInfo() {
		MoitorThreadinfoResult result = new MoitorThreadinfoResult();
		List<MoitorThreadinfo> list = new ArrayList<MoitorThreadinfo>();
		ThreadMXBean tm = ManagementFactory.getThreadMXBean();
		tm.setThreadContentionMonitoringEnabled(true);
		result.setThreadCount(tm.getThreadCount()); // 线程总数
		result.setTotalStartedThreadCount(tm.getTotalStartedThreadCount()); // 线程总数
		long[] tid = tm.getAllThreadIds();
		ThreadInfo[] threadInfos = tm.getThreadInfo(tid, Integer.MAX_VALUE);
		for (int i = 0, length = threadInfos.length; i < length; i++) {
			long threadId = threadInfos[i].getThreadId();
			long cpuTime = tm.getThreadCpuTime(threadId) / 1000;
			MoitorThreadinfo moitorThreadinfo = new MoitorThreadinfo();
			moitorThreadinfo.setId(threadId);
			moitorThreadinfo.setName(threadInfos[i].getThreadName());
			moitorThreadinfo.setState(threadInfos[i].getThreadState().toString());
			moitorThreadinfo.setLockName(threadInfos[i].getLockName());
			moitorThreadinfo.setLockOwnerName(threadInfos[i].getLockOwnerName());
			moitorThreadinfo.setCpuTime(cpuTime);
			list.add(moitorThreadinfo);
		}
		result.setMoitorThreadinfos(list);
		return result;
	}

	

}
